package com.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xiao zeng on 2017/6/26.
 */
@Controller
@RequestMapping("/user")
public class Test {

    @RequestMapping("/index")
    public String index(){
        return "/WEB-INF/tamplate/index";
    }


    @RequestMapping("/show") //带视图渲染方法
    public ModelAndView show(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("test/html;charset=utf-8");
        ModelAndView mv = new ModelAndView("aa");
        mv.addObject("name", "老白");
        mv.addObject("add", new String[]{"北京", "上海"});
        mv.setViewName("WEB-INF/tamplate/index");
        return mv;

    }


    @RequestMapping("/t3")  //转发路径
    public String t1() {
        return "forward:aa.jsp"; //user/aa/jsp
//        return "forward:/aa.jsp";  根目录下 (转发)
//        return "redirect:/aa.jsp"; 重定向到根目录下
//        return  "redirect:aa.jsp"; 重定向到user/aa.jsp
//          return "t3"; 转发到 WEB-INF/tamplate/t3.jsp
    }

    @RequestMapping(value = "/t4", method = RequestMethod.GET) //设置请求格式
    @ResponseBody
    public void t4(HttpServletRequest req) {
        System.out.println(req.getParameter("id"));
    }


    @RequestMapping(value = "/t5/{id}", method = RequestMethod.GET)
    @ResponseBody //请求路径：localhost://user/t5/12.action(Restful 风格)
    public void t5(@PathVariable int id) {
        System.out.println(id);
    }

    //模型方法传值到jsp页面
    @RequestMapping("/t6 ")
    public String t6(Model r) {
        r.addAttribute("name", "jack");
        r.addAttribute("add", new String[]{"北京", "天津"});
        return "WEB-INF/tamplate/index";
    }

    //带默认参数的get传值(可以不写默认参数)
    @RequestMapping(value = "/t7", method = RequestMethod.GET)
    public void t7(@RequestParam(value = "nn", required = false, defaultValue = "老白") String name, @RequestParam(value = "age", required = false, defaultValue = "1") int uage, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print(name);
        out.print(uage);
        System.out.println(name);
        System.out.println(uage);
    }

    @RequestMapping("/t8")
    @ResponseBody //转换json格式（fastjson）
    public String t8() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "老白");
        map.put("age", "55");
        map.put("addr", new String[]{"北京", "上海", "郑州"});
        list.add(map);
        return JSON.toJSONString(list);

    }

    //获取服务器时间
    @RequestMapping("/dataer.action") @ResponseBody
     public String t9(){
         Date d = new Date();
         SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-HH : hh:mm:ss ");
         return sdf.format(d);
     }


}
